package com.zk.demo.service.impl;

import com.zk.demo.bean.User;
import com.zk.demo.mapper.UserMapper;
import com.zk.demo.service.UserService;
import com.zk.demo.util.ResultCode;
import com.zk.demo.util.ResultResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import static com.zk.demo.util.ResultCode.FAILEDADD;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public User login(User user) {
        return userMapper.findUser(user);

    }

    @Override
    public ResultResponse createUser(User user) {
        //查询数据库中用户名是否存在
        User sysUser = userMapper.findUser(user);
        if (sysUser != null) {
            return ResultResponse.buildFailed(FAILEDADD.getCode(), "用户名重复");
        }

        userMapper.createUser(user);
        return new ResultResponse<>(ResultCode.SUCCESS.getCode(), "创建成功");
    }

    @Override
    public ResultResponse updateUser(User user) {

        userMapper.updateUser(user);
        return new ResultResponse<>(ResultCode.SUCCESS.getCode(), "修改密码成功");
    }
}
